/*
 * Name: Minh-Tuan ta
 * Class: Profile.java
 */

package actions;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.*;

import data.FriendDB;
import data.GroupsDB;
import data.MessageDB;
import data.PostDB;
import data.UserDB;

public class Profile extends Action {

	@Override
	protected void setURLMapping() {
		// TODO Auto-generated method stub
		this.urlMapping = "/Profile";
	}

	@Override
	protected String doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String queryString = "?userID=";
		User user = null;
		
		if (request.getParameter("userID") == null)
		{
			user = (User)session.getAttribute("User");
			queryString += user.getUserID();
			request.setAttribute("posts", PostDB.getPostsOfUser(user.getUserID()));
			request.setAttribute("numOfUnreadMessages", MessageDB.getUnreadMessagesToUser(user.getUserID()).size());
			request.setAttribute("numOfFriendRequests", FriendDB.getFriendRequests(user.getUserID()).size());
		}
		else
		{
			user = UserDB.getUser(Integer.parseInt(request.getParameter("userID")));
			queryString += user.getUserID();
			boolean isFriend = FriendDB.isFriend(((User)session.getAttribute("User")).getUserID(), user.getUserID());
			request.setAttribute("isFriend", isFriend);
			request.setAttribute("isFriendRequestSent", FriendDB.isFriendRequestSent(((User)session.getAttribute("User")).getUserID(), user.getUserID()));
			
			if (isFriend || user.getUserID() == ((User)session.getAttribute("User")).getUserID())
			{
				ArrayList<Post> posts = PostDB.getPostsOfUser(user.getUserID());
				request.setAttribute("posts", posts);
			}
			
			if (user.getUserID() == ((User)session.getAttribute("User")).getUserID())
			{
				request.setAttribute("numOfUnreadMessages", MessageDB.getUnreadMessagesToUser(user.getUserID()).size());
				request.setAttribute("numOfFriendRequests", FriendDB.getFriendRequests(user.getUserID()).size());
			}
		}
		ArrayList<Group> group = GroupsDB.getGroups(0, user.getUserID());
		request.setAttribute("user", user);
		request.setAttribute("friends", FriendDB.getFriends(user.getUserID()));
		request.setAttribute("groups", group);
		
		return "/WEB-INF/views/content/Profile.jsp" + queryString;
	}

	@Override
	protected String doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void setupPage(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setAttribute("pageTitle", "Profile");
		ArrayList<String> stylesheets = new ArrayList<String>();
		stylesheets.add("css/default/profile.css");
		request.setAttribute("stylesheets", stylesheets);
	}

}
